<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9; IE=10" />
  <meta name="keywords" content="locateUser,displayAddress" />
  <title>HERE Maps API Example: Locating the Address of a User</title>
  <!-- Set up constants such as APP ID and token -->
  <script type="text/javascript" src="libs/hereAppIdAndToken.js"></script>
  <!-- Bootstrap jQuery Library -->
  <script type="text/javascript" src="libs/jQl.min.js"></script>
  <!-- Asynchronously the HERE Maps API for JavaScript -->
  <script type="text/javascript" src="libs/hereAsyncLoader.js"
    id="HereMapsLoaderScript"
    data-params="maps"
    data-map-container="mapContainer"
    data-callback="afterMapLoad" >
  </script>
  <link rel="icon" href="http://here.com/favicon.ico"/>
  <!--<link href="http://developer.here.com/html/css/main.css" rel="stylesheet" />-->
</head>
<body>
  <h1>Locating the Address of a User</h1>
  <p>This example finds the location of a user and displays the address on the map.</p>
  <div id="mapContainer" style="width:540px; height:334px;"></div>
<script id="example-code" data-categories="positioning,search,infobubble" type="text/javascript" >
//<![CDATA[
var map,
  infoBubbles;


function displayAddress(data) {
  infoBubbles.openBubble(data.location.address.text,
    data.location.position);
}

function moveRandomly() {
  var viewPort = map.getViewBounds();
  return new nokia.maps.geo.Coordinate(
    viewPort.topLeft.latitude - Math.random() * viewPort.getHeight(),
    viewPort.topLeft.longitude + Math.random() * viewPort.getWidth()
  );
}

function locateUser(map) {
  if (nokia.maps.positioning.Manager) {
    var positioning = new nokia.maps.positioning.Manager();
    positioning.getCurrentPosition(
      function (position) {
        map.set('center', position.coords);
        nokia.places.search.manager.reverseGeoCode({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          onComplete: displayAddress
        });

      },
      // Something went wrong we were unable to retrieve the GPS location
      function (error) {
        var errorMsg = 'Location could not be determined: ',
          errors = ['PERMISSION_DENIED', 'POSITION_UNAVAILABLE', 'TIMEOUT'],
          // Display a random nearby address.
          nearby = moveRandomly();


        if (error.code < 4) {
          errorMsg += errors[error - 1];
        } else {
          errorMsg += 'UNKNOWN ERROR';
        }
        alert(errorMsg);

        nokia.places.search.manager.reverseGeoCode({
          latitude: nearby.latitude,
          longitude: nearby.longitude,
          onComplete: displayAddress
        });
      },
      {maximumAge: 750}
    );
  }
}



function afterMapLoad(theMap) {
  map = theMap;
  infoBubbles = new nokia.maps.map.component.InfoBubbles();
  map.components.add(infoBubbles);
  locateUser(map);
}

//]]>
</script>
<script type="text/javascript" src="libs/prettyprint.js"></script>
</body>
</html>